package com.ai.service.impl;

import com.ai.entity.SysRoleDeptEntity;
import com.ai.mapper.SysRoleDeptMapper;
import com.ai.service.SysRoleDeptService;
import com.ai.web.request.DeptBindRoleReq;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


@Service("sysRoleDeptService")
public class SysRoleDeptServiceImpl  implements SysRoleDeptService {

    @Resource
    private SysRoleDeptMapper sysRoleDeptMapper;

    @Override
    public List<String> getDeptIds(String roleId) {
        LambdaQueryWrapper<SysRoleDeptEntity> queryWrapperDept = Wrappers.<SysRoleDeptEntity>lambdaQuery().select(SysRoleDeptEntity::getDeptId).eq(SysRoleDeptEntity::getRoleId, roleId);
        List<SysRoleDeptEntity> sysRoleDeptEntities = sysRoleDeptMapper.selectList(queryWrapperDept);
        return sysRoleDeptEntities.stream().map(SysRoleDeptEntity::getDeptId).collect(Collectors.toList());
    }

    @Override
    public void bindDept(DeptBindRoleReq deptBindRoleReq) {
        //先删除所有绑定
        sysRoleDeptMapper.delete(Wrappers.<SysRoleDeptEntity>lambdaQuery().eq(SysRoleDeptEntity::getRoleId, deptBindRoleReq.getId()));

        deptBindRoleReq.getDepts().forEach(e->{
            SysRoleDeptEntity sysRoleDept=new SysRoleDeptEntity();
            sysRoleDept.setRoleId(deptBindRoleReq.getId());
            sysRoleDept.setDeptId(e);
            sysRoleDeptMapper.insert(sysRoleDept);
        });

    }

}